﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Drv.Drv
{
    public class RJShortCircuitDrv
    {
        string IP;
        int Port;
        Socket socket;
        public RJShortCircuitDrv(string ip, int port, int timeout)
        {
            try
            {
                IP = ip;
                Port = port;
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ReceiveTimeout = timeout;
                SendTimeout = timeout;
                socket.ReceiveTimeout = ReceiveTimeout;
                socket.SendTimeout = SendTimeout;
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("创建测试仪 {0},{1} 发生异常 ：", IP, Port, ex), false);
            }
        }
        int receiveTimeout;
        public int ReceiveTimeout
        {
            get { return receiveTimeout; }
            set
            {
                receiveTimeout = value;
            }
        }
        int sendTimeout;
        public int SendTimeout
        {
            get { return sendTimeout; }
            set
            {
                sendTimeout = value;
            }
        }

        public bool Connected
        {
            get
            {
                try
                {
                    Ping ping = new Ping();
                    var a = ping.Send(IP);
                    if (a.Status == IPStatus.Success && socket.Connected)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Open()
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.ReceiveTimeout = ReceiveTimeout;
                socket.SendTimeout = SendTimeout;
                socket.Connect(IP, Port);
                Logger.Info(string.Format("打开测试仪{0},{1}", IP, Port), false);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("打开测试仪{0},{1} 发生异常:{2}", IP, Port, ex), false);
                return false;
            }
        }

        public bool Disconnect()
        {
            try
            {
                socket.Close();
                Logger.Info(string.Format("关闭测试仪{0},{1}", IP, Port));
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("关闭测试仪异常 {0},{1} {2}", IP, Port, ex), false);
                return false;
            }
        }

        public int FindCRIndex(byte[] bytes)
        {
            var sign = Encoding.Default.GetBytes("\n")[0];
            for (var i = bytes.Length - 1; i > 0; i--)
            {
                if (bytes[i] == sign)
                    return i;
            }
            return -1;
        }

        public byte[] Read()
        {
            try
            {
                var buff = new byte[10240];
                var len = socket.Receive(buff);
                return buff.Take(len).ToArray();
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("测试仪 {0},{1} 发生异常：{2}", IP, Port, ex.Message), false);
                return null;
            }
        }

        public int Write(byte[] s)
        {
            try
            {
                var len = socket.Send(s);
                return len;
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("测试仪 {0},{1} 写发生异常：{2}", IP, Port, ex), false);
                return 0;
            }
        }
    }

}
